package vn.mobiistar.videoapp.fragment;

import java.util.ArrayList;

import vn.mobiistar.videoapp.R;
import vn.mobiistar.videoapp.activity.MainActivity;
import vn.mobiistar.videoapp.loader.BaseLoader;
import vn.mobiistar.videoapp.loader.BaseResult;
import vn.mobiistar.videoapp.loader.VideoLoader;
import vn.mobiistar.videoapp.other.Define;
import vn.mobiistar.videoapp.other.Video;
import vn.mobiistar.videoapp.other.VideoAdapter;
import vn.mobiistar.videoapp.other.VideoPlayer;
import vn.mobiistar.videoapp.ui.BlockView;
import vn.mobiistar.videoapp.ui.Refreshable;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
public class VideoDetailFragment extends Fragment implements
LoaderCallbacks<BaseResult>, Refreshable {

	protected static final String TAG = "VideoDetailFragment";
	private View 					view;
	private Video 					mVideoDetail;
	private Context 				mContext;
	private ArrayList<Video> 		mKeywordVideo;
	private BlockView 				mBlock;
	private boolean 				mIsReachedBottom;
	private BaseLoader				mLoader;
	private VideoPlayer 			mPlayer;
	private int mRotation;
	private ScrollView mScrollView;

	public VideoDetailFragment(Context context,Video video, VideoPlayer player){
		mVideoDetail = video;
		mContext 	 = context;
		mPlayer      = player;
	}
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    	Display display = ((WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		mRotation = display.getRotation();
    	// neu o che do nam ngang thi khong lam j ca
    	if(mRotation != Surface.ROTATION_90 || mRotation != Surface.ROTATION_270 ) {
	        view = inflater.inflate(R.layout.fragment_videodetail, container, false);
	        
	        // an hien text description
	        final TextView des = ((TextView)view.findViewById(R.id.fvd_description));
	        ((ImageView)view.findViewById(R.id.fvd_desbutton)).setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if(des.getVisibility() == View.GONE){
						des.setVisibility(View.VISIBLE);
						((ImageView)v).setImageDrawable(getResources().getDrawable(R.drawable.ic_arrow_up));
					}else{
						des.setVisibility(View.GONE);
						((ImageView)v).setImageDrawable(getResources().getDrawable(R.drawable.ic_arrow_down));
					}
				}
			})
			;
	        // khoi tao mot so bien
	        mIsReachedBottom = false;
	        mLoader 		 = new VideoLoader(mContext);
			((VideoLoader) mLoader) .setType(Define.BLOCK_TYPE_KEYWORD);
			
	        init(mVideoDetail,true);
	       
			mBlock           = (BlockView) view.findViewById(R.id.fvd_blockview);
			mBlock.setTitle(R.string.videoblocktitle_recommend);
			mBlock.getLoadMore().setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					VideoLoader loader = ((VideoLoader) VideoDetailFragment.this.mLoader);
					if (loader != null
							&& !loader.isLoading()
							&& (!VideoDetailFragment.this.mIsReachedBottom)) {
						loader.nextPage();
						loader.forceLoad();
					}
				}
			});
			mBlock.getList().setOnItemClickListener(new ListView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
					Video item = (Video) ((VideoAdapter) ((ListView) parent).getAdapter()).getItem(position);
					mPlayer.playVideo(item,mRotation,10);
				}
			});
			mScrollView = (ScrollView)view.findViewById(R.id.fvd_scroll);
    	}else{
    		view = new View(getActivity());
    		container.addView(view);
    	}
        return view;
    }
    public void init(Video v, boolean init){
    	if(init == false){
    		mVideoDetail = v;
    		if(mKeywordVideo != null) mKeywordVideo.clear();
    		mKeywordVideo.clear();
    		mBlock.setData(mKeywordVideo);
    		mIsReachedBottom = false;
    		mLoader.reload();
    	}
    	((VideoLoader) mLoader) .setKeyword(mVideoDetail.getKeyword());
    	((TextView)view.findViewById(R.id.fvd_videoname)).setText(mVideoDetail.getName());
    	((TextView)view.findViewById(R.id.fvd_videoviewed)).setText(mVideoDetail.getYoutubeViewed()+" "+getResources().getString(R.string.text_viewed));
    	((TextView)view.findViewById(R.id.fvd_description)).setText(mVideoDetail.getDescription());
    }
    @Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
    	if(mRotation != Surface.ROTATION_90 || mRotation != Surface.ROTATION_270 ) 
    		getLoaderManager().initLoader(Define.BLOCK_TYPE_KEYWORD, null, this);
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public Loader<BaseResult> onCreateLoader(int id, Bundle arg1) {
		mLoader.setProgressNotifiable(mBlock.getLoadingView());
		if(id == Define.BLOCK_TYPE_KEYWORD)
			return mLoader;
		return null;
	}

	@Override
	public void onLoadFinished(Loader<BaseResult> loader,
			final BaseResult result) {
		// TODO Auto-generated method stub
		final int id = loader.getId();
		((MainActivity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if(id == Define.BLOCK_TYPE_KEYWORD) if(((VideoLoader.Result) result).mVideo != null) if(((VideoLoader.Result) result).mVideo.size() > 0) {
					mKeywordVideo =  ((VideoLoader.Result) result).mVideo;
					mBlock.setData(mKeywordVideo);
				} else{
					mIsReachedBottom = true;
					mBlock.setNoButtonLoadMore();
				}
			}
		});
	}
	@Override
	public void onLoaderReset(Loader<BaseResult> arg0) {
	}
	@Override
	public void refreshData() {
	}
}